/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bashe.business.statistics;

import han.bashe.business.adapter.jms.JmsAdapter;
import han.bashe.business.pipeline.Handler;
import han.bashe.business.pipeline.PipelineContext;
import han.bashe.common.config.Config;
import han.bashe.common.entity.BasheEntityConstants;
import han.bashe.common.message.NormalizedMessage;
import han.bashe.common.message.RequestHelper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import v1.statistics.web.bifang.han.StatLogDocument;

/**
 * @Author kent
 */
public class StatHandler extends Handler {

	
	
	@Override
	public void postProcess(PipelineContext ctx) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean preProcess(PipelineContext ctx) {
		
		if(! Config.isStatisticsLogOn())
			return false;
		RequestHelper rh = new RequestHelper(ctx.getRequest());
		if(rh.getServiceCode().equals(Config.getNotifyServiceCode()))
			return false;
		return true;
	}

	@Override
	public void process(PipelineContext ctx) {
		try{
			StatLogDocument doc = StatLogDocument.Factory.newInstance();
			doc.addNewStatLog();
			doc.getStatLog().setBusCode(Config.getConfig().getBusCode());
			RequestHelper rh = new RequestHelper(ctx.getRequest());
			
			doc.getStatLog().setMessageLogId(rh.getLogId());
			doc.getStatLog().setRequestId(rh.getRequestId());
			if(ctx.getResponseTime() != null){
				doc.getStatLog().setResponseTime(String.valueOf(ctx.getResponseTime()));
			}
			//in case error,such as no service code
			if(ctx.getRequestTime() == null){
				ctx.setRequestTime(System.currentTimeMillis());
			}
			doc.getStatLog().setRequestTime(String.valueOf(ctx.getRequestTime()));
			doc.getStatLog().setServiceCode(rh.getServiceCode());
			doc.getStatLog().setVersion(rh.getServiceVersion());
			if(ctx.hasError())
				doc.getStatLog().setStatus(BasheEntityConstants.STATUS_ERROR);
			else
				doc.getStatLog().setStatus(BasheEntityConstants.STATUS_OK);
			if(ctx.isRetry()){
				doc.getStatLog().setIsRetry(true);
			}else{
				doc.getStatLog().setIsRetry(false);
			}
			
			NormalizedMessage m = new NormalizedMessage();
			m.setBody(doc.toString());
			System.out.println(doc.toString());
			
			JmsAdapter.call(m, Config.getStatisticsJmsBinding());
		}catch(Exception e){
			Log logger = LogFactory.getLog(StatHandler.class);
			logger
			.error(
					"StatHandler.process() error :",
					e);
		}

	}

}
